<meta charset='utf-8'>
<?php
class Admin extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model("login_model");
		$this->load->model("model_user","user");
	}
	function index(){
		if($this->session->userdata('logged_in')){
			$user_info= $this->session->userdata('logged_in');
			$username = $user_info['username'];
			$fullname = $user_info['fullname'];
			$this->_data=array(
				'template' => "home/index",
				'fullname' => $fullname,
				);
			$this->load->view("index_admin",$this->_data);
		}else{
			redirect('/admin/login');
		}

	}
	function login(){
		$body_data=array();
		if($this->input->post('login')){
			$user = $this->input->post('user');
			$pass = $this->input->post('pass');
			$err='';
			if(trim($user)==''){
				$err ="<img src='/public/admin/img/error.ico'>Vui lòng nhập tên đăng nhập";
			}else if(trim($pass)==''){
				$err ="<img src='/public/admin/img/error.ico'>Bạn chưa nhập mật khẩu";
			}else{
				$get_user = $this->login_model->get_user_by_id($user);
				if(count($get_user) != 0){
					if(md5($pass)==$get_user['password']){
						$user_info=array(
							'username' => $user,
							'fullname' => $get_user['fullname'],
							);
						$this->session->set_userdata('logged_in',$user_info);
						redirect("/admin");
					}else{
						$err="<img src='/public/admin/img/error.ico'>Sai mật khẩu";
					}
				}else{
					$err="<img src='/public/admin/img/error.ico'>Tài khoản không tồn tại";
				}
			}
			$body_data=array(
				'err' => $err,
				);
		}
		$this->parser->parse("login",$body_data);
	}
	function logout(){
		$this->session->unset_userdata('logged_in');
		redirect('/admin/login');
	}
	function edit_account(){
		$username = $_GET['username'];
		$select = $_GET['select'];
		$data = $_GET['data'];
		switch ($select) {
			case 'name':
				$data=array(
					'fullname' => $data,
					);
				$this->user->update_user($username,$data);
				break;
			case 'address':
				$data = array(
					'address' =>$data,
					);
				$this->user->update_user($username,$data);
				break;
			case 'phone':
				$data = array(
					'phone' =>$data,
					);
				$this->user->update_user($username,$data);
				break;
			case 'email':
				$data = array(
					'email' =>$data,
					);
				$this->user->update_user($username,$data);
				break;
			case 'birthday':
				$birthday = datetoint($data);
				$data = array(
					'birthday' =>$birthday,
					);
				$this->user->update_user($username,$data);
				break;
			
		}
	}
}
?>